package yxy.game.pm2.command.hero;

import yxy.apple.util.concurrent.ThreadPoolManager;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Format;
import yxy.game.pm2.bean.Format.FormatType;
import yxy.game.pm2.bean.Hero;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.FormatMessage;
import yxy.game.pm2.message.HeroMessage;
import yxy.game.pm2.module.NotificationHandle;
import yxy.game.pm2.module.hero.FormatHandle;
import yxy.game.pm2.module.hero.HeroHandle;

import java.util.ArrayList;
import java.util.Date;

/**
 * 布阵 保存布阵
 */
@MessageType(proto = Command.FormatSave, classtype = FormatMessage.FormatSaveRequest.class)
public class CMD_FormatSave extends Command<FormatMessage.FormatSaveRequest> {

    @Override
    public void execute(MessageSource messageSource, FormatMessage.FormatSaveRequest request) {
        Date date = new Date();

        String playerCode = messageSource.getTag();
        int typeNumber = request.getType().getNumber();
        FormatMessage.Format formatrequest = request.getFormat();
        String fu = formatrequest.getFU() == "" ? null : formatrequest.getFU();
        String fm = formatrequest.getFM() == "" ? null : formatrequest.getFM();
        String fd = formatrequest.getFD() == "" ? null : formatrequest.getFD();
        String bu = formatrequest.getBU() == "" ? null : formatrequest.getBU();
        String bm = formatrequest.getBM() == "" ? null : formatrequest.getBM();
        String bd = formatrequest.getBD() == "" ? null : formatrequest.getBD();
        int nb = formatrequest.getNB();

        FormatMessage.FormatSaveResponse.Builder response = FormatMessage.FormatSaveResponse.newBuilder();

        FormatType type = FormatType.forNumber(typeNumber);
        if (type == null) {
            send(messageSource, response, Result.ParamError);
            return;
        }
        if (fu == null && fm == null && fd == null && bu == null && bm == null && bd == null) {
            // 不允许空阵容
            send(messageSource, response, Result.FormatLessHero);// 请至少上阵一名武将
            return;
        }

        FormatHandle formatModule = new FormatHandle();

        Format oldformat = formatModule.get(playerCode, type);
        Format format = formatModule.get(playerCode, type);
        format.setFu(fu);
        format.setFm(fm);
        format.setFd(fd);
        format.setBu(bu);
        format.setBm(bm);
        format.setBd(bd);
        format.setNb(nb);

        ArrayList<Hero> heros = new ArrayList<>();

        Result result = formatModule.check(playerCode, format, heros);
        if (!result.equals(Result.Success)) {
            send(messageSource, response, result);
            return;
        }

        //保存队伍
        Format.set(playerCode, type, format);

        response.setType(yxy.game.pm2.message.FormatMessage.FormatType.forNumber(typeNumber));
        response.setFormat(formatModule.get(format));
        HeroHandle heroModule = new HeroHandle();

        //判断阵容是否有改变
        Boolean isChange = getIsChange(oldformat, format, formatModule, playerCode);


        //获取队伍的战力
        long power = new HeroHandle().getAllPower(heros, format, playerCode, 1, isChange);

        for (Hero hero : heros) {
            try {
                HeroMessage.HeroSimple heroSimple = heroModule.getHeroSimple(playerCode, hero, hero.getPower());
                response.addHeros(heroSimple);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        send(messageSource, response, Result.Success);

        // 推送战力
        if (type.equals(FormatType.普通)) {
            //推送
            new NotificationHandle().notifyBattlePower(playerCode, power);
        }

        {
            // 红点
            Runnable runnable = () -> new HeroHandle().updateHeroTip(playerCode);
            ThreadPoolManager.getInstance().execute(runnable);
        }

        Date date1 = new Date();
        System.out.println("CMD_FormatSave:" + (date1.getTime() - date.getTime()));
    }


    //判断阵容是否有改变
    private Boolean getIsChange(Format oldformat, Format format, FormatHandle formatModule, String playerCode) {
        ArrayList<String> oldList = formatModule.formatCodesList(playerCode, oldformat);
        ArrayList<String> newList = formatModule.formatCodesList(playerCode, format);

        if (oldList.size() == 0) return true;

        if (oldList.size() != newList.size()) return true;

        for (int i = 0; i < newList.size(); i++) {

            if (!oldList.contains(newList.get(i))) return true;

            if (!newList.get(i).equals(oldList.get(i))) return true;
        }
        return false;
    }

    private void send(MessageSource messageSource, FormatMessage.FormatSaveResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}